package com.ning.farm_income_back.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ning.farm_income_back.domain.Agriculturalassets;
import com.ning.farm_income_back.domain.Users;
import com.ning.farm_income_back.dto.AgriculturalassetsDto;
import com.ning.farm_income_back.mapper.AgriculturalassetsMapper;
import com.ning.farm_income_back.mapper.UsersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AssetsService {
    @Autowired
    AgriculturalassetsMapper assetsMapper;
    @Autowired
    UsersMapper usersMapper;

    public PageInfo getPage(AgriculturalassetsDto assets, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize,"asset_id desc ");


        List<AgriculturalassetsDto> list =  assetsMapper.selectBy(assets);

        return new PageInfo<>(list);
    }

    public void saveAssetsInfo(AgriculturalassetsDto assetsDto) {
        Users u = usersMapper.selectByRealName(assetsDto.getOwnerName());
        Agriculturalassets assets = assetsDto;
        assets.setOwnerId(u.getUserId());
        assets.setCreatedAt(new Date());
        assetsMapper.insert(assets);
    }

    public void updateAssetsInfo(AgriculturalassetsDto assetsDto) {
        Users u = usersMapper.selectByRealName(assetsDto.getOwnerName());
        Agriculturalassets assets = assetsDto;
        assets.setOwnerId(u.getUserId());
        assets.setUpdatedAt(new Date());
        assetsMapper.updateByPrimaryKeySelective(assets);
    }

    public void deleteAssetsInfo(Integer id) {
        assetsMapper.deleteByPrimaryKey(id.longValue());
    }
}
